package front

import (
	"github.com/gin-gonic/gin"
	"go-api-template/api/http"
	"go-api-template/api/ws/front"
	"go-api-template/service"
)

// OnlineClientListReq 在线人数
type OnlineClientListReq struct {
}
type OnlineClientListResp struct {
	Items []OnlineClientListRespItem `json:"items"`
}
type OnlineClientListRespItem struct {
	UserId  string             `json:"user_id"`
	Clients []front.ClientData `json:"clients"`
}

func (req *OnlineClientListReq) Request(ctx *gin.Context, s *service.Service) (http.Resp, error) {
	return req.Do(ctx, s)
}
func (req *OnlineClientListReq) Do(ctx *gin.Context, s *service.Service) (resp http.Resp, err error) {
	var data OnlineClientListResp

	data.Items = make([]OnlineClientListRespItem, 0)

	OnlineClientListRespM := map[string][]front.ClientData{}

	for c, _ := range front.HUB.C {
		if v, ok := OnlineClientListRespM[c.Data.UserId]; ok {
			OnlineClientListRespM[c.Data.UserId] = append(v, *c.Data)
		} else {
			OnlineClientListRespM[c.Data.UserId] = []front.ClientData{*c.Data}
		}
	}
	for k, v := range OnlineClientListRespM {
		data.Items = append(data.Items, OnlineClientListRespItem{
			UserId:  k,
			Clients: v,
		})
	}

	resp.SetData(data)
	return
}
